//==================================================================
/// color.h
///
/// Created by Davide Pasca - 2009/10/5
/// See the file "license.txt" that comes with this project for
/// copyright info. 
//==================================================================

#ifndef COLOR_H
#define COLOR_H

#define COLOR_SWAP_R_B

//==================================================================
class color
{
public:
	U8	rgba[4];

	color()
	{
	}

	color( U8 r, U8 g, U8 b, U8 a=255 )
	{
		rgba[0] = r;
		rgba[1] = g;
		rgba[2] = b;
		rgba[3] = a;
	}

	color( int r, int g, int b, int a=255 )
	{
		if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
		if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
		if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
		if ( a < 0 ) a = 0; else if ( a > 255 ) a = 255;
		rgba[0] = r;
		rgba[1] = g;
		rgba[2] = b;
		rgba[3] = a;
	}

	color( const U8 rgba_[4] )
	{
		rgba[0] = rgba_[0];
		rgba[1] = rgba_[1];
		rgba[2] = rgba_[2];
		rgba[3] = rgba_[3];
	}

	operator int () const
	{
#ifdef COLOR_SWAP_R_B
		return (rgba[3] << 24) | (rgba[2] << 16) | (rgba[1] << 8) | rgba[0];
#else
		return (rgba[3] << 24) | (rgba[0] << 16) | (rgba[1] << 8) | rgba[2];
#endif
	}

	operator U32 () const
	{
#ifdef COLOR_SWAP_R_B
		return (rgba[3] << 24) | (rgba[2] << 16) | (rgba[1] << 8) | rgba[0];
#else
		return (rgba[3] << 24) | (rgba[0] << 16) | (rgba[1] << 8) | rgba[2];
#endif
	}

	//==================================================================
	static color make_inpt( const color &col1, const color &col2, float t )
	{
		float	tt = 1 - t;

		int r = (int)(col1.rgba[0] * tt + col2.rgba[0] * t);
		int g = (int)(col1.rgba[1] * tt + col2.rgba[1] * t);
		int b = (int)(col1.rgba[2] * tt + col2.rgba[2] * t);
		int a = (int)(col1.rgba[3] * tt + col2.rgba[3] * t);

		return color( r, g, b, a );
	}
};

#endif
